<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2020/8/6 14:39
// +----------------------------------------------------------------------

namespace app\single\command;


use app\single\model\PlayerOnlineModel;
use app\single\model\RoleModel;
use think\Command;
use think\Db;

class XianyuStore extends Command
{
    protected function execute($input, $output)
    {
        $roleModel = new RoleModel();
        $onlineModel = new PlayerOnlineModel();
        $data = [];
        $day = date("Ymd",strtotime($this->sDate));
        $eDate = date("Ymd",strtotime($this->eDate));
        while($day <= $eDate){
            //玩家身上总元宝数
            $this->createData($day, $roleModel->statXianyuStore(), $data, ["total_gold" => "gold"]);
            //元宝产出，消耗
            $goldData = Db::table("c_stat_xianyu")
                ->where("day", $day)
                ->field("platform,operate,SUM(gold_num) AS gold")
                ->group("platform,operate")
                ->select();
            $goldArr = [];
            foreach($goldData as $vo){
                $goldArr[$vo["platform"]]["platform"] = $vo["platform"];
                $goldArr[$vo["platform"]][$vo["operate"]] = $vo["gold"];
            }
            $this->createData($day, $goldArr, $data, ["output_gold" => 1, "consume_gold" => 2]);
            //统计15天内累计登录过3天的玩家的元宝数
            $this->createData($day, $onlineModel->statActiveRoleGold($day), $data, ["active_players" => "players", "active_gold" => "gold"]);
            $day = date('Ymd',strtotime($day) + 86400);
        }
        $this->insertData($data);
    }

    /**
     * 统计数据入库
     * @param int    $day              日期
     * @param array $data             查询数据
     * @param array $saveData        组装数据
     * @param array $fieldArr         字段对应
     * @return array $saveData
     */
    private function createData($day, $data, &$saveData, $fieldArr = array()): array
    {
        foreach($data as $row){
            //查询是否已经有了
            $map["day"] = $day;
            $map["platform"] = $row["platform"];
            $index = implode("#",$map);
            //充值插入的字段
            foreach($fieldArr as $field => $change){
                $saveData[$index][$field] = $row[$change] ?? 0;
                unset($row[$change]);
            }
        }
        return $saveData;
    }

    /**
     * 数据入库
     * @param $data
     */
    private function insertData($data)
    {
        foreach($data as $index => $row){
            list($day, $platform) = explode("#",$index);
            //查询是否已经有了
            $checkInfo = Db::table("c_xianyu_store")
                ->where("day", $day)
                ->where("platform", $platform)
                ->find();
            if($checkInfo["id"] > 0){
                //更新
                Db::table("c_xianyu_store")->where("id", $checkInfo["id"])->update($row);
            }else{
                $addArr = $row;
                $addArr["day"] = $day;
                $addArr["platform"] = $platform;
                Db::table("c_xianyu_store")->insert($addArr);
            }
        }
    }
}